package com.das.mechanic_base.mvp.presenter.create;

import com.das.mechanic_base.base.X3BasePresenter;
import com.das.mechanic_base.bean.common.AwsFileBean;
import com.das.mechanic_base.bean.live.LiveSelectCarBean;
import com.das.mechanic_base.bean.main.CarDriLicBean;
import com.das.mechanic_base.bean.main.CarPicAndColorBean;
import com.das.mechanic_base.bean.main.CarPoolMainBean;
import com.das.mechanic_base.bean.main.CarSelectBean;
import com.das.mechanic_base.bean.main.HomeMainCarBean;
import com.das.mechanic_base.bean.main.UpadateReceBean;
import com.das.mechanic_base.bean.main.UpdateReceiveCarBean;
import com.das.mechanic_base.bean.main.ValidCarBean;
import com.das.mechanic_base.mapi.api.NetWorkHttp;
import com.das.mechanic_base.mapi.response.HttpCallBack;
import com.das.mechanic_base.mapi.schedulers.RxSchedulersHelper;
import com.das.mechanic_base.mvp.contract.create.X3CreateServiceContract;
import com.das.mechanic_base.utils.X3StringUtils;

import java.io.File;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class X3CreateServicePresenter extends X3BasePresenter<X3CreateServiceContract.View> implements X3CreateServiceContract.Presenter {

    private UpdateReceiveCarBean carBean;

    @Override
    public void getStoreIconList(List<String> mList) {
        NetWorkHttp.getApi().listStoreCarIcon(mList)
                .compose(mView.bindToLife())
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .subscribe(new HttpCallBack<List<String>>() {
                    @Override
                    protected void onDone(List<String> datalist) {
                        mView.getStoreIconListSuccess(datalist);
                    }

                    @Override
                    protected String LoadingMessage() {
                        return null;
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });
    }

    @Override
    public void getStoreCarNumReceive(HashMap<String, Object> map) {
        NetWorkHttp.getApi().listStoreCarNumReceive(map)
                .compose(mView.bindToLife())
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .subscribe(new HttpCallBack<List<LiveSelectCarBean>>() {
                    @Override
                    protected void onDone(List<LiveSelectCarBean> datalist) {
                        if(X3StringUtils.isListEmpty(datalist)){
                            return;
                        }
                        mView.getStoreCarNumReceiveSuccess(datalist);

                    }

                    @Override
                    protected String LoadingMessage() {
                        return null;
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });
    }

    @Override
    public void listStoreAllCarNum() {
        NetWorkHttp.getApi().listStoreAllCarNum()
                .compose(mView.bindToLife())
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .subscribe(new HttpCallBack<CarPoolMainBean>() {
                    @Override
                    protected void onDone(CarPoolMainBean carPoolMainBean) {
                        mView.listStoreAllCarSuccess(carPoolMainBean);
                    }

                    @Override
                    protected String LoadingMessage() {
                        return null;
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });
    }

    @Override
    public void searchByCarNum(String carNum) {
        NetWorkHttp.getApi().validCarInfo(carNum)
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .compose(mView.bindToLife())
                .subscribe(new HttpCallBack<ValidCarBean>() {
                    @Override
                    protected void onDone(ValidCarBean searchNumBean) {
                        mView.getCarInfo(searchNumBean);
                    }

                    @Override
                    protected String LoadingMessage() {
                        return null;
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });
    }

    @Override
    public void saveReceiveBase(String carNum) {

        NetWorkHttp.getApi().saveReceiveBase(carNum)
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .compose(mView.bindToLife())
                .subscribe(new HttpCallBack<UpdateReceiveCarBean>() {
                    @Override
                    protected void onDone(UpdateReceiveCarBean receiveCarBean) {
                        carBean = receiveCarBean;
                        mView.createReceiveSuccess(receiveCarBean.getId(),carNum);
                    }

                    @Override
                    protected String LoadingMessage() {
                        return null;
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });

    }

    @Override
    public void getReceiveBase(long receiveId) {

        NetWorkHttp.getApi().getReceiveBase(receiveId)
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .compose(mView.bindToLife())
                .subscribe(new HttpCallBack<HomeMainCarBean>() {
                    @Override
                    protected void onDone(HomeMainCarBean mainCarBean) {
                        mView.getReceiveBase(mainCarBean);
                    }

                    @Override
                    protected String LoadingMessage() {
                        return null;
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });
    }

    @Override
    public void updateReceiveCar(CarSelectBean carSelectBean, String carNum) {
        if (carBean == null) {
            carBean = new UpdateReceiveCarBean();
        }
        //参数 车型
        if (!X3StringUtils.isEmpty(carSelectBean.getCarStyleName())) {
            carBean.setCarSeriesId(carSelectBean.getCarTypeId());
            String carStyleName = carSelectBean.getCarStyleName();
            String[] split = carStyleName.split("\\(");
            carBean.setCarSeriesName(split[0]);
        }
        //参数 车辆品牌
        if (!X3StringUtils.isEmpty(carSelectBean.getCarBrand())) {
            carBean.setCarBrandId(carSelectBean.getCarBrandId());
            carBean.setCarBrandName(carSelectBean.getCarBrand());
        }
        //参数年款
        if (!X3StringUtils.isEmpty(carSelectBean.getCarYear())) {
            carBean.setCarStyleId(carSelectBean.getCarStyleId());
            carBean.setCarStyleName(carSelectBean.getCarYear());
        }
        carBean.setCarVin(carSelectBean.getVin() + "");

        carBean.setColorId(carSelectBean.getColorId());

        carBean.setCarPicTempletId(carSelectBean.getCarPicTempletId());

        if (!X3StringUtils.isEmpty(carSelectBean.getCarMiles())) {
            carBean.setEstimateCurrentMiles(carSelectBean.getCarMiles());
        }
        if (!X3StringUtils.isEmpty(carSelectBean.getPerMiles())) {
            carBean.setPerMiles(carSelectBean.getPerMiles());
        }

        carBean.setCarProductionDate(carSelectBean.getCarProductionDate());

        carBean.setWaitingForCarPicTemplet(carSelectBean.isWaitingForCarPicTemplet());

        if (!X3StringUtils.isEmpty(carNum)) {
            carBean.setCarNum(carNum);
        }

        if (!X3StringUtils.isEmpty(carSelectBean.getCarOwnerName())) {
            carBean.setCarOwnerName(carSelectBean.getCarOwnerName());
        }
        if (!X3StringUtils.isEmpty(carSelectBean.getGender())) {
            carBean.setGender(carSelectBean.getGender());
        }

        if (!X3StringUtils.isEmpty(carSelectBean.getMobile())) {
            carBean.setMobile(carSelectBean.getMobile());
        }

        if (carSelectBean.getCarTypeId() != 0) {
            carBean.setCarTypeId(carSelectBean.getCarTypeId());
        }
        if (!X3StringUtils.isEmpty(carSelectBean.getLastName())) {
            carBean.setLastName(carSelectBean.getLastName());
        }

        if (!X3StringUtils.isEmpty(carSelectBean.getFirstName())) {
            carBean.setFirstName(carSelectBean.getFirstName());
        }

        if (!X3StringUtils.isEmpty(carSelectBean.getLastName())) {
            carBean.setLastName(carSelectBean.getLastName());
        }

        carBean.setSalutation(carSelectBean.getSalutation());
        carBean.setCustomSet(carSelectBean.getCustomSet());

        carBean.setChatroomBaseIdList(new ArrayList<>());

        carBean.setId(carSelectBean.getId());

        NetWorkHttp.getApi().updateReceiveCar(carBean)
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .compose(mView.bindToLife())
                .subscribe(new HttpCallBack<UpadateReceBean>() {
                    @Override
                    protected void onDone(UpadateReceBean upadateReceBean) {
                        upadateReceBean.setFirstName(carBean.getFirstName());
                        mView.updateReceiveBaseSuccess(upadateReceBean);
                    }

                    @Override
                    protected String LoadingMessage() {
                        return null;
                    }

                    @Override
                    protected void NoNetCallBack() {
                        super.NoNetCallBack();
                        mView.updateReceiveBaseFailed();
                    }

                    @Override
                    protected void showError(String error) {
                        mView.updateReceiveBaseFailed();
                    }
                });


    }

    @Override
    public void deleteReceiveCar(long receiveId) {

        NetWorkHttp.getApi().deleteReceiveBase(receiveId)
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .compose(mView.bindToLife())
                .subscribe(new HttpCallBack<Object>() {
                    @Override
                    protected void onDone(Object o) {

                    }

                    @Override
                    protected String LoadingMessage() {
                        return null;
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });

    }

    @Override
    public void uploadHeaderAws(File imgFile, long carId) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), imgFile);
        try {
            String filename = URLEncoder.encode(imgFile.getName(), "UTF-8");
            MultipartBody.Part fileBody = MultipartBody.Part.createFormData("multipartFile", filename, requestBody);

            Map<String,RequestBody> map = new HashMap<>();

            map.put("description",RequestBody.create(MediaType.parse("multipart/form-data"), "车主头像"));

            map.put("ossBucketName",RequestBody.create(MediaType.parse("multipart/form-data"), "SERVICE_PIC"));

            NetWorkHttp.getApi().uploadFileAws(map,fileBody,new HashMap<>())
                    .compose(RxSchedulersHelper.defaultComposeRequest())
                    .compose(mView.bindToLife())
                    .subscribe(new HttpCallBack<AwsFileBean>() {
                        @Override
                        protected void onDone(AwsFileBean awsFileBean) {
                            if (awsFileBean != null){
                                updateCarOwnerUserPortrait(carId,awsFileBean.id);
                            }
                        }

                        @Override
                        protected String LoadingMessage() {
                            return null;
                        }

                        @Override
                        protected void showError(String error) {

                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void updateCarOwnerUserPortrait(long carId, long resourceId) {
        NetWorkHttp.getApi().updateCarOwnerUserPortrait(carId, resourceId)
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .compose(mView.bindToLife())
                .subscribe(new HttpCallBack<String>() {
                    @Override
                    protected void onDone(String str) {
                        //上传成功
                        mView.uploadHeaderSuccess();
                    }

                    @Override
                    protected String LoadingMessage() {
                        return null;
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });
    }

    @Override
    public void requestCarNum(File file) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file);
        try {
            String filename = URLEncoder.encode(file.getName(), "UTF-8");
            MultipartBody.Part body = MultipartBody.Part.createFormData("file", filename, requestBody);

            NetWorkHttp.getApi().requestCarNum(body)
                    .compose(RxSchedulersHelper.defaultComposeRequest())
                    .compose(mView.bindToLife())
                    .subscribe(new HttpCallBack<String>() {
                        @Override
                        protected void onDone(String carNum) {
                            mView.getCarNum(carNum);
                        }

                        @Override
                        protected String LoadingMessage() {
                            return null;
                        }

                        @Override
                        protected void showError(String error) {

                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void requestDriLicNum(File file) {

        RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file);
        try {
            String filename = URLEncoder.encode(file.getName(), "UTF-8");
            MultipartBody.Part body = MultipartBody.Part.createFormData("file", filename, requestBody);

            NetWorkHttp.getApi().vehicleLicenseRecognize(body)
                    .compose(RxSchedulersHelper.defaultComposeRequest())
                    .compose(mView.bindToLife())
                    .subscribe(new HttpCallBack<CarDriLicBean>() {
                        @Override
                        protected void onDone(CarDriLicBean driLicBean) {
                            mView.onGetDriLicNum(driLicBean);
                        }

                        @Override
                        protected String LoadingMessage() {
                            return null;
                        }

                        @Override
                        protected void showError(String error) {

                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void requestVin(File file) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file);
        try {
            String filename = URLEncoder.encode(file.getName(), "UTF-8");
            MultipartBody.Part body = MultipartBody.Part.createFormData("file", filename, requestBody);
            NetWorkHttp.getApi().vinRecognition(body)
                    .compose(RxSchedulersHelper.defaultComposeRequest())
                    .compose(mView.bindToLife())
                    .subscribe(new HttpCallBack<String>() {
                        @Override
                        protected void onDone(String vin) {
                            mView.getVin(vin);
                        }

                        @Override
                        protected String LoadingMessage() {
                            return null;
                        }

                        @Override
                        protected void showError(String error) {
                            mView.getVinFail(error);
                        }
                    });

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void uploadCarPic(long carId, String imgPath,String carNum) {

        File imgFile = new File(imgPath);

        if (!imgFile.exists()) {
            return;
        }

        RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), imgFile);
        try {
            String filename = URLEncoder.encode(imgFile.getName(), "UTF-8");
            MultipartBody.Part fileBody = MultipartBody.Part.createFormData("multipartFile", filename, requestBody);

            NetWorkHttp.getApi().createCarPic(carId,carNum,fileBody)
                    .compose(RxSchedulersHelper.defaultComposeRequest())
                    .compose(mView.bindToLife())
                    .subscribe(new HttpCallBack<CarPicAndColorBean>() {
                        @Override
                        protected void onDone(CarPicAndColorBean colorBean) {
                            mView.uploadCarPicSuccess(colorBean);
                        }

                        @Override
                        protected String LoadingMessage() {
                            return null;
                        }

                        @Override
                        protected void showError(String error) {
                            mView.uploadCarPicSuccess(null);
                        }
                    });

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
